--
-- Author: ping.wang
-- Date: 2018-10-16 11:52:47
--
-- AuctionBuyListRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AuctionNormalBuyListRender))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self._order = self._ui.order
    self._num = self._ui.num
    self._icon = self._ui.gold
    self._stock = self._ui.stock
    self._buy = self._ui.buy

    local iconId = tonumber(GD:getSystemPar( "auctionNormalGoodsLootType",99))
    local coinConfig = GD:getCoinConfig(iconId)
    local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.bigicon)
    self._ui.gold:loadTexture(iconPath)

    self._totalCount = 0
    self:setContentSize( self._ui.Panel:getContentSize() )
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data
        self._order:setString(string.format(L("lua_code_text_413"),data.idx))
        self._num:setString(tostring(data.price))
        self._stock:setString(tostring(data.stock))
        self._totalCount = data.stock
        self._buy:setString(tostring(data.buyNum))
        --批次 单价 库存 购入
        self:_adjustIconPosX(self._icon,self._num)
        --购入的数量会变化
    else
        self:setVisible( false )
    end
end

--基于文本调整ICON的位置
function ClassRef:_adjustIconPosX(icon,text)
    local parentPanelW = text:getParent():getContentSize().width/2
    local fixedGoldenPosX = parentPanelW + text:getContentSize().width/2 + icon:getContentSize().width * 0.6
    icon:setPositionX(fixedGoldenPosX)
end

function ClassRef:setBuyNum(num)
    self._buy:setString(tostring(num))
end

function ClassRef:getTotalCount()
    return self._totalCount
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
